import axios from 'axios'
import qs from 'qs'
import {baseURL} from './config.js'


export default {
    get(url, params, config) {
        return new Promise((resolve, reject) => {
            axios.get(url, {
                params: params,
                baseURL: config?.baseURL ?? baseURL ?? '', 
                timeout: 100000
            }).then(response => {
                resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    post(url, params, config) {
        return new Promise((resolve, reject) => {
            axios.post(url, qs.stringify(params), {
                baseURL: config?.baseURL ?? baseURL ?? '',
                timeout: 100000,
            }).then(response => {
                resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    delete(url, params, config) {
        return new Promise((resolve, reject) => {
            axios.delete(url, { 
                params: params,
                baseURL: config?.baseURL ?? baseURL ?? '',
                timeout: 100000
            }).then(response => {
                    resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    put(url, params, config) {
        return new Promise((resolve, reject) => {
            axios.put(url, { 
                params: params,
                baseURL: config?.baseURL ?? baseURL ?? '',
                timeout: 100000,
            }).then(response => {
                resolve(response.data)
            }) .catch(error => {
                reject(error)
            })
        })
    },
    postJSON(url, params, config) {
        return new Promise((resolve, reject) => {
            axios.post(url, params, {
                baseURL:  config?.baseURL ?? baseURL ?? '',
                timeout: 100000,
                headers: {
                    'Content-Type': 'application/json'  //如果写成contentType会报错
                }
            }).then(response => {
                resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    upload(url, params, config) {
        let fd = new FormData()
        for (var key in params) {
            fd.append(key, params[key]);
        }
        return new Promise((resolve, reject) => {
            axios.post(url, fd, {
                baseURL:  config?.baseURL ?? baseURL ?? '',
                timeout: 100000,
                headers: { 'Content-Type': 'multipart/form-data' }
            }).then(response => {
                resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    uploadArticle(url, params, config) {
        let fd = new FormData()
        for (var key in params) {
            if ('files' == key){
                let files = params[key];
                files.forEach(item =>{
                    fd.append("files", item);
                })
            }else if ('coverImgs' == key){
                let files = params[key];
                files.forEach(item =>{
                    fd.append("coverImgs", item);
                })
            }else {
                fd.append(key, params[key]);
            }
        }
        return new Promise((resolve, reject) => {
            axios.post(url, fd, {
                baseURL:  config?.baseURL ?? baseURL ?? '',
                timeout: 100000,
                headers: { 'Content-Type': 'multipart/form-data' }
            }).then(response => {
                resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    }
}
